home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / PNL Libraries / MyNotifier.p < prev    next >
Encoding:
Text File  |  1995-10-23  |  5.7 KB  |  240 lines  |  [TEXT/CWIE]

  1. unit MyNotifier;
  2.  
  3. { Derived from <jholt@adobe.COM> Joe Holt's StartupError code as posted }
  4. { to comp.sys.mac.programmer in May 1991 }
  5.  
  6. { Notification Manager messages }
  7.  
  8. { History: }
  9. {   jhh 18 jun 90 -- response to news posting }
  10. {   pnl 29 may 91 -- Converted to pascal to be used in an application }
  11.  
  12. interface
  13.  
  14.     uses
  15.         Types, Memory;
  16.  
  17.     const
  18.         mark_app = 1;
  19.         mark_none = 0;
  20.  
  21.     procedure StartupNotifier;
  22.     procedure NotifyH (mark: integer; sound: handle; sicn: handle; str: stringPtr; display_time: longInt);
  23.     procedure Notify (mark, sound: boolean; sicn_id, sicn_index, str_id, str_index: integer; display_time: longInt);
  24. { mark - mark the current application }
  25. { sound - play sysbeep }
  26. { sicn_id, sicn_index - SICN id to rotate with the apple & index (<1 -> 1)   OR 0&0 for no sicn }
  27. { str_id, str_index - STR# id & index    OR    STR id & 0    OR    0 & 0 }
  28.     procedure UnNotify;
  29. { Call this to get rid of the notification }
  30.  
  31.     var
  32.         notify_finished, notify_outstanding: boolean;
  33.         time_to_unnotify: longInt;
  34.  
  35. implementation
  36.  
  37.     uses
  38.         Types, Notification, GestaltEqu, Icons, OSUtils, TextUtils, Resources,
  39.         MyStartup, MySystemGlobals;
  40.  
  41.     const
  42.         sicn_size = 32;
  43.         T_NMInstall = $A05E;
  44.         T_Unimplemented = $A89F;
  45.  
  46.     type
  47.         NMRecPtrPtr = ^NMRecPtr;
  48.         booleanPtr = ^boolean;
  49.  
  50.     var
  51.         current_note: NMRecPtr;
  52.  
  53.     var
  54.         gMyResponseProc : UniversalProcPtr;
  55.         
  56. { handles must be non-purgeable, but may be unlocked }
  57.  
  58.     procedure MyResponse (note: NMRecPtr);
  59.     begin
  60.         booleanPtr(note^.nmRefCon)^ := true;
  61.     end;
  62.  
  63.     procedure UnNotify;
  64.         var
  65.             oe: OSErr;
  66.     begin
  67.         if current_note <> nil then begin
  68.             oe := NMRemove(current_note);
  69.             with current_note^ do begin
  70.                 if nmStr <> nil then begin
  71.                     DisposePtr(pointer(nmStr));
  72.                 end;
  73.                 if nmIcon <> nil then begin
  74.                     DisposeHandle(nmIcon);
  75.                 end;
  76.             end;
  77.             DisposePtr(pointer(current_note));
  78.             current_note := nil;
  79.         end;
  80.         notify_finished := false;
  81.         notify_outstanding := false;
  82.         time_to_unnotify := maxLongInt;
  83.     end;
  84.  
  85.     procedure NotifyH (mark: integer; sound: handle; sicn: handle; str: stringPtr; display_time: longInt);
  86.         var
  87.             error: boolean;
  88.             oe: OSErr;
  89.     begin
  90.         UnNotify;            { Clear outstanding notify }
  91.         if NGetTrapAddress(T_NMInstall, OSTrap) = NGetTrapAddress(T_Unimplemented, ToolTrap) then begin
  92.             SysBeep(1);   { Best we can do I guess.  Could put up the dialog box maybe?...}
  93.         end
  94.         else begin
  95.             current_note := NMRecPtr(NewPtr(sizeof(NMRec)));
  96.             if current_note = nil then begin
  97.                 SysBeep(1);   { Can't do much else if there isnt even room for this! }
  98.             end
  99.             else begin
  100.                 with current_note^ do begin
  101.                     qType := nmType;
  102.                     error := false;
  103.                     booleanPtr(nmRefCon) := @notify_finished;
  104.                     nmMark := mark;
  105.                     nmStr := str;
  106.                     nmIcon := sicn;
  107.                     nmSound := sound;
  108.                     nmResp := gMyResponseProc;
  109.                 end;
  110.                 oe := NMInstall(current_note);
  111.                 if oe <> noErr then begin
  112.                     current_note := nil;
  113.                     SysBeep(1);
  114.                 end
  115.                 else begin
  116.                     notify_outstanding := true;
  117.                     if display_time > 0 then begin
  118.                         time_to_unnotify := TickCount + display_time;
  119.                     end;
  120.                 end;
  121.             end;
  122.         end;
  123.     end;
  124.  
  125.     procedure Notify (mark, sound: boolean; sicn_id, sicn_index, str_id, str_index: integer; display_time: longInt);
  126.         var
  127.             errorText: str255;
  128.             sh: stringHandle;
  129.             sicnH: handle;
  130.             error: boolean;
  131.             nmMark: integer;
  132.             nmStr: stringPtr;
  133.             nmIcon: handle;
  134.             nmSound: handle;
  135.             gv: longInt;
  136.     begin
  137.         error := false;
  138.         if mark then begin
  139.             nmMark := 1;
  140.         end else begin
  141.             nmMark := 0;
  142.         end;
  143.         nmStr := nil;
  144.         if str_id <> 0 then begin
  145.             if str_index > 0 then begin
  146.                 GetIndString(errorText, str_id, str_index);
  147.             end
  148.             else begin
  149.                 errorText := '';
  150.                 sh := GetString(str_id);
  151.                 if sh <> nil then begin
  152.                     if sh^ <> nil then begin
  153.                         errorText := sh^^;
  154.                     end;
  155.                     ReleaseResource(handle(sh));
  156.                 end;
  157.             end;
  158.             if errorText = '' then begin
  159.                 error := true;
  160.             end else begin
  161.                 nmStr := stringPtr(NewPtr(length(errorText) + 1));
  162.                 if nmStr = nil then begin
  163.                     error := true;
  164.                 end else begin
  165.                     nmStr^ := errorText;
  166.                 end;
  167.             end;
  168.         end;
  169.         nmIcon := nil;
  170.         if sicn_id <> 0 then begin
  171.  
  172.             nmIcon := nil;
  173.             if (Gestalt(gestaltSystemVersion, gv) = noErr) & (gv >= $0700) then begin
  174.                 if GetIconSuite(nmIcon, sicn_id, svAllSmallData) <> noErr then begin
  175.                     nmIcon := nil;
  176.                 end;
  177.             end;
  178.             if nmIcon = nil then begin
  179.                 if sicn_index < 1 then begin
  180.                     sicn_index := 1;
  181.                 end;
  182.                 sicn_index := (sicn_index - 1) * sicn_size;   { 1-based, like STR# }
  183.                 sicnH := GetResource('SICN', sicn_id);
  184.                 HNoPurge(sicnH);
  185.                 if sicnH = nil then begin
  186.                     error := true;
  187.                 end else begin
  188.                     nmIcon := NewHandle(sicn_size);
  189.                     if nmIcon = nil then begin
  190.                         error := true;
  191.                     end else if nmIcon^ = nil then begin
  192.                         error := true;
  193.                     end else if GetHandleSize(sicnH) < sicn_index + sicn_size then begin
  194.                         error := true;
  195.                     end else begin
  196.                         BlockMoveData(ptr(longInt(sicnH^) + sicn_index), nmIcon^, sicn_size);
  197.                     end;
  198.                     ReleaseResource(sicnH);
  199.                 end;
  200.             end;
  201.         end;
  202.         if sound or error then begin
  203.             nmSound := handle(-1);
  204.         end else begin
  205.             nmSound := nil;
  206.         end;
  207.         NotifyH(nmMark, nmSound, nmIcon, nmStr, display_time);
  208.     end;
  209.  
  210.     function InitNotifier(var msg: integer): OSStatus;
  211.     begin
  212.         msg := msg; { Unused }
  213.         current_note := nil;
  214.         notify_finished := false;
  215.         notify_outstanding := false;
  216.         time_to_unnotify := maxLongInt;
  217.         gMyResponseProc := NewNMProc(@MyResponse);
  218.         InitNotifier := noErr;
  219.     end;
  220.  
  221.     procedure FinishNotifier;
  222.     begin
  223.         if current_note <> nil then begin
  224.             UnNotify;
  225.         end;
  226.     end;
  227.  
  228.     procedure IdleNotifier;
  229.     begin
  230.         if (notify_finished and InForeground) or (TickCount > time_to_unnotify) then begin
  231.             UnNotify;
  232.         end;
  233.     end;
  234.  
  235.     procedure StartupNotifier;
  236.     begin
  237.         SetStartup(InitNotifier, IdleNotifier, 10, FinishNotifier);
  238.     end;
  239.     
  240. end.